<?php

session_start();

if (isset($_POST['logOutButton'])) {
	logout();
}

if (isset($_SESSION['loggedIn'])){	
	loggedInPage();
} 
 
else if (isset($_REQUEST['attempt']))
{
		$username = $_POST['username'];
		$password = $_POST['password'];	
	
	
	if ($username == "Admin" && $password == "Password" ){
			
		$_SESSION['username'] = $username;
		$_SESSION['password'] = $password;
			
			$errors = 'Inloggning lyckades';
		if(isset($_POST['checkbox'])){
			$errors .= ' och dina uppgifter är sparade';
		}
		loggedInPage($errors);
	}
	else{
		if(empty($username) && empty($password)){
			$errors = 'Användarnamn saknas';
		}
		else if(empty($username)){
			$errors = 'Användarnamn saknas!';
		}
		else if(empty($password)){
			$errors = 'Lösenor saknas';
		}
		else{
			$errors = 'Felaktigt användarnamn och/eller lösenord';
		}
		printLoginForm($errors);
	}
}
else
{
	printLoginForm();		
}
function printLoginForm($errors = null) {
	
 $html = '<!DOCTYPE html>
 		<head>
 		<meta charset="utf-8">
 		<title>Benny är Awesome!</title>
 		</head>
 		<body>
 		<h1>Ej Inloggad!</h1>
 		';
	
	if ($errors != null){
		$html .= $errors;
		$errors = null;
	}
	$usernameValue = null;
	if (isset($_POST['username'])) {
		$usernameValue = $_POST['username'];	
	}
 $html .= 
 		"<form id='logInForm' action='index.php?attempt' method='post'>
 		Användarnamn: 
 		<input type='text' Name='username' id='username' value='$usernameValue' />";		
 $html .=	
 		'Lösenord: 
 		<input type="password" Name="password" id="password" />
 		<input type="checkbox" name="checkbox" value="1" id="checkbox"/>Remember Me!
 		<button type="submit">Log In</button>
 		</form>
 		'. theTime() .'
 		</body>
 		</html>';
 echo $html;
}

function loggedInPage($errors = null){
 		
		$_SESSION['loggedIn'] = TRUE;
		
 		$html2 = '<!DOCTYPE html>
 		<meta charset="utf-8">
    	<body>';
		
	if ($errors != null){
		$html2 .= $errors;
		$errors = null;
		}
	
 $html2 .='	
 		<h1>Admin är inloggad!</h1>
 		<form action="index.php" method="post">
 			<button type="buttton" name="logOutButton" 
 			id="logOutButton">Log Out
 			</button>
		</form>
		'. theTime() .'
 		</body>
 		</html>
 		';
 
 echo $html2;
}

function logout(){
		
	session_destroy();
	$errors = 'Du har loggats ut';
    printLoginForm($errors);
	exit;
} 

function theTime(){
		
	$daynum = date("j");
	$year   = date("Y");
	$hours  = date("H");
	$minutes= date("i");
	$seconds= date("s");

 	$day = getDay();
	$month = getMonth();
 
	$klocka = $day . " den " . $daynum . " " . $month . "  år " . $year .
			" Klockan är [". $hours . ":" . $minutes . ":" . $seconds . "].";

return $klocka;
}

function getDay(){
		
	$day    = date("l");
	
	switch($day)
	{
	case "Monday":    $day = "Måndag";  break;
	case "Tuesday":   $day = "Tisdag"; break;
	case "Wednesday": $day = "Onsdag";  break;
	case "Thursday":  $day = "Torsdag"; break;
	case "Friday":    $day = "Fredag";  break;
	case "Saturday":  $day = "Lördag";  break;
	case "Sunday":    $day = "Söndag";  break;
	}
	
	return $day;
}

function getMonth(){
	
	$month  = date("F");
	
	switch($month)
	{
	case "January":   $month = "Januari";    break;
	case "February":  $month = "Februari";   break;
	case "March":     $month = "Mars";     break;
	case "April":     $month = "April";     break;
	case "May":       $month = "Maj";       break;
	case "June":      $month = "Juni";      break;
	case "July":      $month = "Juli";      break;
	case "August":    $month = "Augusti";    break;
	case "September": $month = "September"; break;
	case "October":   $month = "Oktober";   break;
	case "November":  $month = "November";  break;
	case "December":  $month = "December";  break;
	}
	
	return $month;
}
 
 
 
 
 
 